package com.wangzg.module.admin.controller;


import com.wangzg.module.admin.model.kuaidi.KuaiDi;
import com.wangzg.module.admin.model.kuaidi.Response;
import com.wangzg.module.admin.model.kuaidi.ResponseData;
import com.wangzg.module.admin.service.KuaiDiQueryService;
import com.wangzg.module.admin.service.KuaiDiService;
import com.wangzg.common.util.CommonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("kuaidi_query/")
public class KuaiDiQueryController {

    @Autowired
    private KuaiDiService kuaiDiService;
    @Autowired
    private KuaiDiQueryService kuaiDiQueryService;

    /**
     * 返回json数据
     * @param com
     * @param no
     * @return
     */
    @GetMapping("/getKuaiDiInfoByJson")
    @ResponseBody
    public String queryKuadiInfoByJson(String com, String no) {
        return kuaiDiQueryService.synQueryData(com, no,"", "", "", 0);
    }

    @GetMapping("/{phone}")
    @ResponseBody
    public Response queryKuaidiByPhone(@PathVariable("phone") String phone){
        Response response = new Response();
        if(StringUtils.isNotEmpty(phone)){
            List<ResponseData> responseDataList = new ArrayList<>();
            //  1.通过手机号查询下面的所有订单号
            List<KuaiDi> kuaiDiList = kuaiDiService.getList("", phone,"");
            if(!CollectionUtils.isEmpty(kuaiDiList)){
                kuaiDiList.forEach(kuaiDi -> {
                    //  2.依次查出所有的订单号
                    String responseDataStr = kuaiDiQueryService.synQueryData(kuaiDi.getCompany(), kuaiDi.getKuaidiNo(),"", "", "", 0);
                    ResponseData responseData = CommonUtils.convertJsonStr2Object(responseDataStr);
                    responseDataList.add(responseData);
                });
            }
            // 3.组装数据返回给前台
            response.setDataList(responseDataList);
        }
        return response;
    }

    @GetMapping("/show/{kuaidi_no}")
    @ResponseBody
    public ResponseData queryKuaidiByNo(@PathVariable("kuaidi_no") String kuaidiNo){
        ResponseData responseData  = new ResponseData();
        if(StringUtils.isNotEmpty(kuaidiNo)){
            //  1.通过手机号查询下面的所有订单号
            List<KuaiDi> kuaiDiList = kuaiDiService.getList("","", kuaidiNo);
            if(!CollectionUtils.isEmpty(kuaiDiList)){
                KuaiDi kuaiDi = kuaiDiList.get(0);
                String responseDataStr = kuaiDiQueryService.synQueryData(kuaiDi.getCompany(), kuaiDi.getKuaidiNo(),"", "", "", 0);
                responseData = CommonUtils.convertJsonStr2Object(responseDataStr);
            }
        }
        return responseData;
    }


}
